#include "GameCC.h"

extern GameControlCenter GameCC;

void BoardControlCenter::askQuestion(Player* player, int catNo)
{
   Category* cat;
   if (catNo == 0)
	   cat = GameCC.CatCC.getRandom();
   else
      cat = GameCC.CatCC.item(catNo-1);
	printf("\nEnter the value of the next element in sequence.\n");
   printf("\n[%s]\n", cat->strName);

   int queNo = GetRandomNumber(cat->getCount()-1);
   char* answer;
   answer = GetString(cat->item(queNo));
   player->nTotal++;

   int ansNo = cat->find(answer);
   if (ansNo >= 0 && ansNo == queNo+1)
   {
      puts("Correct!");
      player->nCorrect++;
   }
   else
   {
      puts("Incorrect...");
      printf("\nThe correct answer is: %s\n\n", cat->item(queNo+1));
   }
}

void BoardControlCenter::run()
{
	ClearScreen();
	char* name = GetString("Enter your name: ");
	if (!strlen(name)) name = "New Player";
	Player* player = GameCC.PlayersCC.add(name, 0, 0);
	printf("Welcome, %s!\n", name);
   
   int catNo;
   while(true)
   {
      GameCC.CatCC.print(true);
      char* answer;
      answer = GetString("Select a category");
      catNo = atoi(answer);
      if (catNo >=0 && catNo <= GameCC.CatCC.getCount())
         break;
      puts("Incorrect category");
   }
	
	bool CanContinue = true;
	while(CanContinue)
	{
		askQuestion(player, catNo);		
		switch (WaitForKey("Would you like to continue? (Y/N)"))
		{
			case 'Y': case 'y': break;
         default: CanContinue = false; 
		}
      system("cls");
	}

   player->save();
}